﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BestBuy.DAL;

namespace BestBuy.PageGuest
{
    public partial class cCompanyProfile : System.Web.UI.Page
    {
        CProduct _csp = new CProduct();
        CBuyerOffer _cb = new CBuyerOffer();
        CSellerOffer _cs = new CSellerOffer();
        CDanhMuc _cdm = new CDanhMuc();
        CThongTinCaNhan _cttcn = new CThongTinCaNhan();
        CCongTy _ct = new CCongTy();
        private int _idcongty;
        protected void Page_Load(object sender, EventArgs e)
        {
            LoadInFo();
        }

        private string MainMarkets(string thitruong) {
            string str = "";
            String[] strmang = thitruong.Split('+');
            if (strmang[0] == "1")
            {
                str += "Americas <br />";
            }
            if (strmang[1] == "1")
            {
                str += "Asia <br /> ";
            }
            if (strmang[2] == "1")
            {
                str += "Middle East <br />";
            }
            if (strmang[3] == "1")
            {
                str += "Wordwide <br />";
            }
            if (strmang[4] == "1")
            {
                str += "Africa <br />";
            }
            if (strmang[4] == "1")
            {
                str += "Europe <br />";
            }
            return str;
        }

        private void LoadInFo()
        {
            if (Request.Params["pid"] != null)
            {
                try
                {
                    _idcongty = Convert.ToInt32(Request.Params["pid"].ToString());
                    //load thong tin sp
                    ThongTinTaiKhoan tk = _cttcn.GetAcountOfCompany(_idcongty);
                    
                    image.ImageUrl = "~/Images/Product/" + tk.CongTy.LogoCongTy;
                    lbName.Text = tk.CongTy.TenCongTy;
                    lbCountry.Text = tk.QuocGia.TenQuocGia;
                    lbYear.Text = tk.CongTy.NamThanhLap.ToString();
                    lbBusiness.Text = tk.CongTy.LoaiCongTy;
                    lbMainMarkets.Text = MainMarkets(tk.CongTy.ThiTruongChinh);
                    lbProducts.Text = tk.CongTy.TuKhoaSanPham;
                    lbYearEstablished.Text = tk.CongTy.NamThanhLap.ToString();
                    lbNumberEmployees.Text = tk.CongTy.SoLuongNhanVien;
                    lbLocation.Text = tk.CongTy.KhuVucNhaMay;
                    lbFactorySize.Text = tk.CongTy.KichThuocNhaMay;
                    lbRepresentative.Text = tk.CongTy.NguoiDaiDien;
                    lbIndustry.Text = tk.CongTy.DanhMuc.TenDanhMuc;
                    imgCountry.ImageUrl = "~/Images/Flags/" + tk.QuocGia.TenQuocGia + ".gif";
                    dlLatestProducts.DataSource = _csp.GetTop5OfIDAcount(tk.IDTaiKhoan);
                    dlLatestProducts.DataBind();
                    
                    //load menu quick
                    hlProduct.NavigateUrl = "~/PageGuest/PageProductOfAccount.aspx?aid=" + tk.IDTaiKhoan;
                    hlBuy.NavigateUrl = "~/PageGuest/PageBuyOfAccount.aspx?aid=" + tk.IDTaiKhoan;
                    hlSell.NavigateUrl = "~/PageGuest/PageSellOfAccount.aspx?aid=" + tk.IDTaiKhoan;

                    //load linkQuick
                    DanhMuc danhmuc = _ct.GetCatalogyOfIDCompany(_idcongty);
                    HyperLink hl = new HyperLink();
                    hl.ID = "hl" + _idcongty.ToString();
                    hl.NavigateUrl = "~/PageGuest/cListOfCategoryLevel1.aspx?pid=" + danhmuc.IDDanhMuc;
                    int count = _cdm.GetQuantityOfCategoryProduct(_idcongty) + danhmuc.SanPhams.Count;
                    hl.Text = danhmuc.TenDanhMuc;
                    Label lb = new Label();
                    lb.ID = "lb" + _idcongty.ToString();
                    lb.Text = " > " + tk.CongTy.TenCongTy;
                    linkQuick.Controls.Add(hl);
                    linkQuick.Controls.Add(lb);

                    //load count product,sell,buy
                    lbCountProduct.Text = _csp.GetCountProductOfID(tk.IDTaiKhoan).ToString();
                    lbCountBuy.Text = _cb.GetCountBuyOfID(tk.IDTaiKhoan).ToString();
                    lbCountSell.Text = _cs.GetCountSellOfID(tk.IDTaiKhoan).ToString();

                }
                catch
                {
                    Response.Redirect("~/PageGuest/PageError.aspx");
                }
            }
            else
            {
                Response.Redirect("~/PageGuest/PageError.aspx");
            }
        }
        public string CheckHinhAnh(object obj)
        {
            string strHinh = "";
            if (obj != null)
            {
                strHinh = "~/Images/Product/" + obj.ToString();
            }
            return strHinh;
        }
        protected void lnkPopup_Click(object sender, EventArgs e)
        {
            imgFull.ImageUrl = image.ImageUrl;
            mpeSanPham.Show();
        }

        protected void btnContact_Click(object sender, EventArgs e)
        {
            Response.Redirect("PageContactMessage.aspx?ml=4&pid=" + _idcongty.ToString());
        }

        
    }
}